package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;

import cn.itcast.pojo.Role;

import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {


    @Reference
    UserService userService;

    @RequestMapping("/getUsername.do")
    public User getUsername() {
        //获取当前用户名


        User user = (User) SecurityContextHolder.getContext().
                getAuthentication().getPrincipal();



        return user;
    }


    /**
     * 用户管理-分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

      return userService.findPage(queryPageBean);

    }


    /**
     * 用户管理-新增用户
     * @param user
     * @return
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody cn.itcast.pojo.User user){

        try {

            userService.add(user);

        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false, MessageConstant.ADD_USER_FAIL);

        }

        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }


    /**
     * 用户管理-根据用户id查询对应的角色
     * @param id
     * @return
     */
    @RequestMapping("/findRoleIdByUserId.do")
    public Result findRoleIdByUserId(Integer id){

        try {

            List<Role> roles = userService.findRoleIdByUserId(id);


            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,roles);


        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false, MessageConstant.QUERY_USER_FAIL);

        }

    }

    /**
     *  用户管理-给用户添加角色
     * @param uid
     * @param roleIds
     * @return
     */
    @RequestMapping("/addRole.do")
    public Result addRole(Integer uid, @RequestBody Integer[] roleIds){

        try {

          userService.addRole(uid,roleIds);


            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);


        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false, MessageConstant.ADD_ROLE_FAIL);

        }

    }

    /**
     * 用户管理-根据id删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteUserById.do")
    public Result deleteUserById(Integer id){

        try {

            userService.deleteUserById(id);


            return new Result(true, "删除用户成功");


        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false, "删除用户失败");

        }

    }

    @RequestMapping("/findByRoleId.do")
    public Result findByRoleId(int id){

        try {
            List<cn.itcast.pojo.User> list = userService.findByRoleId(id);
            ArrayList<Map> list1 = new ArrayList<>();
            for (cn.itcast.pojo.User user : list) {
                HashMap<String, String> map = new HashMap<>();
                map.put("value",user.getUsername());
                list1.add(map);
            }

            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,list1);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false, MessageConstant.QUERY_USER_FAIL);

        }

    }


}
